<?php

declare(strict_types=1);

namespace app\model;

use app\api\controller\Coupon;
use app\common\facade\auth\Auth;
use app\model\base\BaseCouponCateModel;
use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id
 * @property string $name 分类名称
 * @property string $origin_id 第三方id
 * @property int $status 状态(1:启用,0:关闭)
 * @property string $created_at
 * @property string $updated_at
 * @property string $mark 第一次导入聚推客使用匹配，上线后可删除
 * @property int $sort 排序
 * @property string $deleted_at 删除时间
 * @property-read \app\model\CouponBrand[] $coupon_brand
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 */
class CouponCate extends BaseCouponCateModel
{
    use SoftDelete;

    const STATUS_ACTIVE = 1; //激活
    const STATUS_CLOSE = 0; //关闭

    /**
     * @descripe 获取分类关联品牌
     * @scenc api接口用到
     */
    public function getCateAndSubBrand(){
        self::with('');
    }

    /**
     * @descripe 关联关系-品牌
     */
    public function couponBrand(){

        return $this->belongsToMany(CouponBrand::class,'coupon_brand_cate_relation','brand_id','cate_id');

    }


    /**
     * 分类列表
     */
    public static function cateList($needPag = true){
        $field = '*';
        $query = self::query()->order('created_at', 'desc');
        if (Auth::from() == 'agent') {
            $field = 'id as cate_id, name as cate_name';
            $query->where('status', self::STATUS_ACTIVE);
        }
        if (!$needPag) {
            return $query->field($field)->select()->toArray();
        }
        return $query->field($field)->paginate()->toArray();
    }


    /**
     *   分类下拉列表
     */
    public static function cateSelectList($params){
        $list = self::query()->where('status', self::STATUS_ACTIVE)
            ->field('id as cate_id, name as cateName')
            ->order('created_at', 'desc')
            ->select();
        return $list;
    }

    /**
     * 修改分类
     * */
    public static function saveCate($data,$scene){
        $validate = new \app\admin\validate\Coupon\Cate();
        $result = $validate->scene($scene)->check($data);
        if(!$result) return fail($validate->getError());
        if(!empty($data['id'])) {
            $cate = self::where("id", $data['id'])->find();
            if (empty($cate)) return fail('分类编号错误！');
            self::update($data);
        }else{
            $cate=new self();
            $cate->save($data);
        }
        return success("设置成功！");
    }

    /**
     *  删除分类
     */
    public static function delCate($data){
        if(empty($data['id'])) return fail("分类编号不能为空");
        self::destroy($data['id']);
        return  success("删除成功！");
    }

    public function selectlinkageCateBrand($cate_id)
    {
        return CouponBrand::where('status', 1)->when($cate_id,function($query) use ($cate_id){
            $query->join('coupon_brand_cate_relation b', 'coupon_brand.id = b.brand_id',"left")->where("b.cate_id", $cate_id);
        })->with(['coupon_cate'])->column('name', 'id');
    }
}
